<?php defined('PATH') or exit();

class Model_StoreRecord extends SimpleO {

	const STATUS_NOTDELIVERED = 0;
	const STATUS_DELIVERED = 1;

	private $data = array('id' => null, 'fromacc' => null, 'fromguid' => null, 'toguid' => null, 'itemid' => null, 'container' => null, 'time' => null, 'count' => null, 'subcount' => null, 'status' => null, 'type' => null, 'offer_id' => null, 'fromname' => null);

	public function __construct() {
		parent::__construct();
	}

	public function __get($_key) {
		if (is_array($this->data) && array_key_exists($_key, $this->data))
			return $this->data[$_key];
	}

	public function __set($_key, $_value) {
		if (is_array($this->data) && array_key_exists($_key, $this->data))
			$this->data[$_key] = $_value;
	}

	public function load($_id) {
		$this->data = $this->db->query('SELECT * FROM `__archez_store_record` WHERE `__archez_store_record`.`id` = '.$this->db->quote($_id))->fetch();
		if ($this->data) {
			return true;
		}
		return false;
	}

	public function save() {
		if ($this->id)
			return (bool) $this->db->query('UPDATE `__archez_store_record` SET `fromacc` = '.$this->db->quote($this->fromacc).' AND `fromguid` = '.$this->db->quote($this->fromguid).' AND `toguid` = '.$this->db->quote($this->toguid).' AND `itemid` = '.$this->db->quote($this->itemid).' AND `container` = '.$this->db->quote($this->container).' AND `count` = '.$this->db->quote($this->count).' AND `subcount` = '.$this->db->quote($this->subcount).' AND `status` = '.$this->db->quote($this->status).' AND `type` = '.$this->db->quote($this->type).' AND `offer_id` = '.$this->db->quote($this->offer_id).' AND `fromname` = '.$this->db->quote($this->fromname).' WHERE `id` = '.$this->db->quote($this->id));
		$this->db->query('INSERT INTO `__archez_store_record` (`fromacc`, `fromguid`, `toguid`, `itemid`, `container`, `time`, `count`, `subcount`, `status`, `type`, `offer_id`, `fromname`) VALUES ('.$this->db->quote($this->fromacc).', '.$this->db->quote($this->fromguid).', '.$this->db->quote($this->toguid).', '.$this->db->quote($this->itemid).', '.$this->db->quote($this->container).', '.time().', '.$this->db->quote($this->count).', '.$this->db->quote($this->subcount).', '.$this->db->quote($this->status).', '.$this->db->quote($this->type).', '.$this->db->quote($this->offer_id).', '.$this->db->quote($this->fromname).')');
		$id = $this->db->lastInsertId();
		if ($id) {
			$this->id = $id;
			return true;
		}
		return false;
	}

	public function delete() {
		if ($this->id) {
			 $this->db('DELETE FROM `__archez_store_record` WHERE `id` = '.$this->db->quote($_id));
			 return true;
		}
		return false;
	}

	public function isLoaded() {
		if ($this->id) {
			return true;
		}
		return false;
	}

}
?>